package com.xms.sdk.client;

import com.xms.core.exception.XmsException;
import com.xms.sdk.core.DynamicData;
import com.xms.sdk.core.query.*;
import com.xms.sdk.query.IAggregateExpressionResolver;
import com.xms.sdk.service.IAggregateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * AggregateService
 * </p>
 *
 * @author migo
 * @since 2021/9/7
 */
@Service
public class AggregateService implements IAggregateService {
    private IAggregateExpressionResolver _aggregateExpressionResolver;
    /*private IRoleEntityPermissionService _roleEntityPermissionService;
    private IEntityFinderService _entityFinderService;
    private ICurrentUser _currentUser;*/

    @Autowired
    public AggregateService(
            /*ICurrentUser currentUser,
            IRoleEntityPermissionService roleObjectAccessEntityPermissionService,
            IEntityFinderService entityFinderService,*/
            IAggregateExpressionResolver aggregateExpressionResolver)
    {
        /*_roleEntityPermissionService = roleObjectAccessEntityPermissionService;
        _entityFinderService = entityFinderService;
        _currentUser = currentUser;*/
        _aggregateExpressionResolver = aggregateExpressionResolver;
    }

    /*private void bindUserEntityPermissions(QueryExpression query)
    {
        if (!this._currentUser.isAdministrator() && !this._currentUser.getRoles().isEmpty())
        {
            List<UUID> roles = this._currentUser.getRoles();
            List<String> entities = QueryExpressionExt.getAllEntityNames(query, null);
            List<UUID> entIds = _entityFinderService.getByNames(entities).stream().map(Entity::getEntityid).collect(Collectors.toList());
            this._currentUser.setRoleEntityPermissions(_roleEntityPermissionService.getRolePermissions(entIds, roles, AccessRightValue.Read));
        }
    }*/

    public List<DynamicData> execute(AggregateExpression agg) throws SQLException, XmsException {
        //bindUserEntityPermissions(agg);
        return _aggregateExpressionResolver.execute(agg);
    }

    public List<DynamicData> execute(QueryExpression query, Map<String, AggregateType> attributeAggs, List<String> groupFields) throws SQLException, XmsException {
        //bindUserEntityPermissions(query);
        return _aggregateExpressionResolver.execute(query, attributeAggs, groupFields);
    }

    public List<DynamicData> groupingTop(int top, String groupField, QueryExpression query, OrderExpression order) throws XmsException, SQLException {
        //bindUserEntityPermissions(query);
        return _aggregateExpressionResolver.groupingTop(top, query, groupField, order);
    }

    public long count(String entityName, FilterExpression filter) throws SQLException, XmsException {
        Object value = getValue(entityName, filter, AggregateType.Count);
        if (value != null)
        {
            return Long.parseLong(value.toString());
        }
        return 0;
    }

    public double sum(String entityName, FilterExpression filter) throws SQLException, XmsException {
        Object value = getValue(entityName, filter, AggregateType.Sum);
        if (value != null)
        {
            return Double.parseDouble(value.toString());
        }
        return 0;
    }

    public double avg(String entityName, FilterExpression filter) throws SQLException, XmsException {
        Object value = getValue(entityName, filter, AggregateType.Avg);
        if (value != null)
        {
            return Double.parseDouble(value.toString());
        }
        return 0;
    }

    public double max(String entityName, FilterExpression filter) throws SQLException, XmsException {
        Object value = getValue(entityName, filter, AggregateType.Max);
        if (value != null)
        {
            return Double.parseDouble(value.toString());
        }
        return 0;
    }

    public double min(String entityName, FilterExpression filter) throws SQLException, XmsException {
        Object value = getValue(entityName, filter, AggregateType.Min);
        if (value != null)
        {
            return Double.parseDouble(value.toString());
        }
        return 0;
    }

    public Object getValue(String entityName, FilterExpression filter, AggregateType aggregateType) throws SQLException, XmsException {
        AggregateExpression agg = new AggregateExpression();
        agg.EntityName = entityName;
        agg.Criteria = filter;
        AggregateExpressionField aggField = new AggregateExpressionField();
        aggField.AttributeName = entityName + "id";
        aggField.AggregateType = aggregateType;
        agg.AggregateFields.add(aggField);
        agg.AddColumns(entityName + "id");
        List<DynamicData> values = this.execute(agg);
        if (!values.isEmpty())
        {
            return values.get(0).getValue(aggField.AttributeName);
        }
        return null;
    }
}
